var lazyload = {
	curpage:1,
	pagerows:5,
	totalrows:0,
	totalpages:0,
	targetHtml:"",
	requestUrl: "http://op.admin.jufan.tv/api/newsList?callbackname=?",
	init: function(objHtml) {
		this.getList(objHtml);
	},
	getList: function (objHtml){
		var that = this;
		$.getJSON(this.requestUrl,{page:this.curpage, rows:this.pagerows}, function (json){
			try{
				that.totalrows = json.totalRow;
				// if(that.totalrows%that.pagerows == 0){
				// 	that.totalpages = that.totalrows/that.pagerows;
				// }else{
				// 	that.totalpages = that.totalrows/that.pagerows + 1;
				// }
				//that.totalpages = Math.floor((that.totalrows + that.pagerows - 1)/that.pagerows);
				that.totalpages = json.totalPage;
				var dataList = json.list;
				that.showData(dataList, objHtml);

				if(that.curpage == that.totalpages){
					$(".loadmore").html("所有数据加载完成");
				}else{
					$(".loadmore").html("下拉加载更多数据...");
					that.listenScroll(objHtml);
				}
			}catch(e){
				console.log(e)
			}
		})
		// $.ajax({
		// 	type: "get",
		// 	url: that.requestUrl,
		// 	data:"page=" + this.page + "&rows=" + this.rows,
		// 	dataType:"jsonp",
		// 	jsonp:"callbackname",
		// 	success: function (json){
		// 		console.log(json);
		// 	}
		// })
	},
	showData: function (dataList, objHtml){
		var str = "";
		$.each(dataList, function (i, item){
			str += '<li><div class="new-imgs">'+ item.pub_date +'</div><div class="thumbnail"><a href="./news.html?id='+ item.id +'" class="news-title">'+ item.title +'</a><p class="news-text">'+ item.summary+'<a href="./news.html?id='+ item.id +'" id="details">【阅读全文】</a></p><div class="news-time">'+ item.create_time +'</div></div></li>'
		})
		$(str).appendTo(objHtml);
	},
	listenScroll: function (objHtml){
		var that = this;
		$(window).on("scroll", function (){
			if(($(window).scrollTop() + $(window).height()) >= $(document).height()){
				// console.log(that.curpage)
				// console.log(that.totalpages)
				if(that.curpage < that.totalpages){
					that.curpage++;
					that.getList(objHtml);
				}else{
					$(".loadmore").html("所有数据加载完成")
				}
			}
		})
	}
}
lazyload.init(".news-page");
